﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace WFL.Office.Excel
{
    public class ExcelSheets : DisposableSite
    {
        public ISheet this[int index]
        {
            get
            {
                object sheet = _sheets[index];
                if (sheet is Worksheet)
                {
                    ExcelWorksheet item = new ExcelWorksheet((Worksheet)sheet) { Application = Application };
                    Trace(item);
                    return item;
                }
                else if (sheet is Chart)
                {
                    ExcelChart item = new ExcelChart((Chart)sheet);
                    Trace(item);
                    return item;
                }
                else
                {
                    ExcelDialogSheet item = new ExcelDialogSheet((DialogSheet)sheet);
                    Trace(item);
                    return item;
                }
            }
        }

        public int Count
        {
            get { return _sheets.Count; }
        }

        private Sheets _sheets;

        public ExcelApplication Application
        {
            get;
            internal set;
        }

        internal ExcelSheets(Sheets sheets)
        {
            _sheets = sheets;
        }

        protected override void Dispose(bool disposing)
        {
            if (_sheets != null)
            {
                if (disposing)
                {
                    Marshal.ReleaseComObject(_sheets);
                }
                _sheets = null;
                Application = null;
                base.Dispose(disposing);
            }
        }


        #region Add

        public object Add(SheetType type = SheetType.xlWorksheet, object before = null, object after = null, int count = 1)
        {
            object beforeArg = Type.Missing;
            object afterArg = Type.Missing;
            switch (type)
            {
                default:
                    throw new ArgumentException("type");
                case SheetType.xlWorksheet:
                case SheetType.xlExcel4IntlMacroSheet:
                case SheetType.xlExcel4MacroSheet:
                    {
                        if (before != null)
                        {
                            beforeArg = ((ExcelWorksheet)before)._worksheet;
                        }
                        if (after != null)
                        {
                            afterArg = ((ExcelWorksheet)after)._worksheet;
                        }
                        ExcelWorksheet item = new ExcelWorksheet((Worksheet)_sheets.Add(beforeArg, afterArg, count, type)) { Application = Application };
                        Trace(item);
                        return item;
                    }
                case SheetType.xlChart:
                    {
                        if (before != null)
                        {
                            beforeArg = ((ExcelChart)before)._chart;
                        }
                        if (after != null)
                        {
                            afterArg = ((ExcelChart)after)._chart;
                        }
                        ExcelChart item = new ExcelChart((Chart)_sheets.Add(beforeArg, afterArg, count, type));
                        Trace(item);
                        return item;
                    }
                case SheetType.xlDialogSheet:
                    {
                        if (before != null)
                        {
                            beforeArg = ((ExcelDialogSheet)before)._dialogSheet;
                        }
                        if (after != null)
                        {
                            afterArg = ((ExcelDialogSheet)after)._dialogSheet;
                        }
                        ExcelDialogSheet item = new ExcelDialogSheet((DialogSheet)_sheets.Add(beforeArg, afterArg, count, type));
                        Trace(item);
                        return item;
                    }
            }
        }

        public object Add(string template, object before = null, object after = null, int count = 1)
        {
            object beforeArg = Type.Missing;
            object afterArg = Type.Missing;
            if (before != null)
            {
                if (before is ExcelWorksheet)
                {
                    beforeArg = ((ExcelWorksheet)before)._worksheet;
                }
                else if (before is ExcelChart)
                {
                    beforeArg = ((ExcelChart)before)._chart;
                }
                else if (before is ExcelDialogSheet)
                {
                    beforeArg = ((ExcelDialogSheet)before)._dialogSheet;
                }
                else
                {
                    throw new ArgumentException("before");
                }
            }
            if (after != null)
            {
                if (after is ExcelWorksheet)
                {
                    afterArg = ((ExcelWorksheet)after)._worksheet;
                }
                else if (after is ExcelChart)
                {
                    afterArg = ((ExcelChart)after)._chart;
                }
                else if (after is ExcelDialogSheet)
                {
                    afterArg = ((ExcelDialogSheet)after)._dialogSheet;
                }
                else
                {
                    throw new ArgumentException("after");
                }
            }
            object sheet = _sheets.Add(beforeArg, afterArg, count, template);
            if (sheet is Worksheet)
            {
                ExcelWorksheet item = new ExcelWorksheet((Worksheet)sheet) { Application = Application };
                Trace(item);
                return item;
            }
            else if (sheet is Chart)
            {
                ExcelChart item = new ExcelChart((Chart)sheet);
                Trace(item);
                return item;
            }
            else
            {
                ExcelDialogSheet item = new ExcelDialogSheet((DialogSheet)sheet);
                Trace(item);
                return item;
            }
        }

        #endregion

    }
}
